﻿//-----------------------------------------------------------------------
// <copyright file="Addresses.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business.User
{
    using System;
    using System.Collections.Generic;
    using Delta.Data.DataExpress;

    /// <summary>
    /// Collection of addresses
    /// </summary>
    [Serializable]
    public class Addresses : List<Address>
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Addresses class
        /// </summary>
        public Addresses()
        {
        }
        #endregion

        #region Static Load Methods
        /// <summary>
        /// Load addresses by owner
        /// </summary>
        /// <param name="owner">Address owner</param>
        /// <param name="showDeleted">Show deleted</param>
        /// <returns>Collection of addresses</returns>
        public static Addresses Load(IUser owner, bool showDeleted)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("OwnerGuid", owner.Guid));
            parameters.Add(new Parameter("Deleted", showDeleted));
            return DataAccess.GetCollection<Addresses, Address>("LoadAddresses", System.Data.CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Get Prefered Address Methods
        /// <summary>
        /// Get the prefered billing address
        /// </summary>
        /// <returns>Prefered billing address</returns>
        public Address GetPreferedBilling()
        {
            foreach (Address address in this)
            {
                if (address.PreferedBilling)
                {
                    return address;
                }
            }

            return null;
        }

        /// <summary>
        /// Get the prefered delivery address
        /// </summary>
        /// <returns>Prefered delivery address</returns>
        public Address GetPreferedDelivery()
        {
            foreach (Address address in this)
            {
                if (address.PreferedDelivery)
                {
                    return address;
                }
            }

            return null;
        }
        #endregion
    }
}
